﻿using Baidu.Aip.Speech;
using CatUtils.Utils.Currency.IoUtils;
using System;
using System.Collections.Generic;
using System.IO;

namespace CatUtils.Utils.SpecialUtils.Speech.BaiduSdk
{
    /*
     ffmpeg -y  -i bili.flv -acodec pcm_s16le -ss 00:00:50 -t 00:01:40  -f s16le -ac 1 -ar 16000 16k.pcm

        根据官方文档配置相关数据。
    视频音频转码文档：    http://ai.baidu.com/docs#/ASR-Tool-convert/top
         */

    internal class BaiduSpeechTool
    {
        public Asr InitClient()
        {// 设置APPID/AK/SK
            var APP_ID = "";
            var API_KEY = "";
            var SECRET_KEY = "";

            Asr client = new Asr(APP_ID, API_KEY, SECRET_KEY)
            {
                Timeout = 60000  // 修改超时时间
            };

            return
                  client;
        }

        public void Start()
        {
            string FilePath = FileHelper.SelectFile();
            if (FilePath == "")
                return;
            AsrData(InitClient(), FilePath);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="Client"></param>
        /// <param name="FilePath"></param>
        public void AsrData(Asr Client, string FilePath)
        {
            var data = File.ReadAllBytes(FilePath);
            Client.Timeout = 120000; // 若语音较长，建议设置更大的超时时间. ms
            Dictionary<string, object> Options = new Dictionary<string, object>();
            Options.Add("dev_pid", "1537");
            var result = Client.Recognize(data, FileNameHelper.GetFileType(FilePath).Replace(".", ""), 16000, Options);
            Console.Write(result);
        }
    }
}